package in.ac.dei.edrp.cms.controller.settings;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import in.ac.dei.edrp.cms.dao.settings.SettingService;
import in.ac.dei.edrp.cms.domain.login.Login;
import in.ac.dei.edrp.cms.domain.studentmaster.StudentMasterInfoBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class SettingController extends MultiActionController{
	
	private SettingService settingService;

	public void setSettingService(SettingService settingService) {
		this.settingService = settingService;
	}
	
	public ModelAndView getUserAddress(HttpServletRequest request,HttpServletResponse response)
	{
		HttpSession session = request.getSession();

		if (session.getAttribute("universityId") == null) {
			return new ModelAndView("general/SessionInactive",
					"sessionInactive", true);
		}
		
		List<StudentMasterInfoBean> addressData=settingService.getUserAddress(session.getAttribute("userId").toString());
		
		return new ModelAndView("studentmaster/addressInfo", "details",addressData);
	}
	
	public ModelAndView changeUserAddress(HttpServletRequest request,HttpServletResponse response)
	{
		HttpSession session = request.getSession();

		if (session.getAttribute("universityId") == null) {
			return new ModelAndView("general/SessionInactive",
					"sessionInactive", true);
		}
		
		List<StudentMasterInfoBean> addressBeanList = new ArrayList<StudentMasterInfoBean>();
		
		StringTokenizer addresses = new StringTokenizer(
				request.getParameter("address"), "|");
		StringTokenizer cities = new StringTokenizer(
				request.getParameter("city"), "|");
		StringTokenizer states = new StringTokenizer(
				request.getParameter("state"), "|");
		StringTokenizer pinCodes = new StringTokenizer(
				request.getParameter("pin"), "|");
		int i = 0;

		while (cities.hasMoreTokens()) {
			
			StudentMasterInfoBean addressBean = new StudentMasterInfoBean();
			addressBean.setUserId(session.getAttribute("userId").toString());
			addressBean.setStudentId(session.getAttribute("userId").toString());
			
			if (i == 0) {
				addressBean.setAddressKey("PER");
				addressBean.setOfficePhone(request.getParameter("officePhone"));
				addressBean.setHomePhone(request.getParameter("homePhone"));
				addressBean.setOtherPhone(request.getParameter("otherPhone"));
				addressBean.setFax(request.getParameter("fax"));
			} else {
				addressBean.setAddressKey("COR");
			}

			addressBean.setState(states.nextToken());
			addressBean.setCity(cities.nextToken());

			if (addresses.hasMoreTokens()) {
				addressBean.setAddressLineOne(addresses.nextToken());
			}

			if (pinCodes.hasMoreTokens()) {
				addressBean.setPinCode(pinCodes.nextToken());
			}
			i++;
			addressBeanList.add(addressBean);
		}
		
		Boolean isUpdated=settingService.changeUserAddress(addressBeanList);
		return new ModelAndView("enrollment/info", "info", isUpdated);
	}
	
	public ModelAndView changePassword(HttpServletRequest request,HttpServletResponse response)
	{
		HttpSession session = request.getSession();

		if (session.getAttribute("universityId") == null) {
			return new ModelAndView("general/SessionInactive",
					"sessionInactive", true);
		}
		
		Login login=new Login();
		login.setUserName(session.getAttribute("userName").toString());
		login.setPassword(request.getParameter("password"));
		login.setNewPassword(request.getParameter("newPassword"));
		
		Boolean isUpdated=settingService.changeUserPassword(login);
		
		return new ModelAndView("enrollment/info", "info", isUpdated);
	}
}
